Repository navigation
fix(cuda): let the graph-exact Mamba1 scan take float32 A with f16 x - #3128
Conversation
f16 Mamba and Jamba keep A = -exp(A_log) in float32 because f16 cannot hold it (#3046, #3057). The CUDA graph-exact scan port refused any input whose dtype differed from x, so f16 Jamba on CUDA (pre-Ampere default, or MLXCEL_CUDA_F16_NORMALIZE on Ampere) fell back to the per-timestep graph scan. mamba1_scan_kernel_accepts now admits exactly one extra mix on the graph-exact port: f16 x with float32 A, every other input in f16. The launcher passes A in float32 for that pair and names its dtype as a TA template arg (CUDA only; Metal and ROCm sources and cache keys are unchanged). The kernel's new mamba1_decay mirrors the graph scan's three ops for the pair: delta promoted to float32 and multiplied with one rounding (__fmul_rn), exp in float32, then the decay cast to f16 as MLX's CastOp does. Uniform-dtype inputs take the same ops as before. The dtype rule is also exported on dtype codes (mamba1_graph_exact_scan_accepts_dtypes) so a test checks it on every backend. CUDA-gated tests check bit identity with the graph scan for the mixed pair, the predicate, and that an f16 Jamba takes the kernel with logits equal to the forced graph scan. Mamba's CUDA forward still uses the per-step graph scan by design; a test pins that its f16 scan inputs are accepted. ssm_a_f32 and ssm_a_from_log move from mamba.rs to a new models::ssm_a module shared by Mamba, Jamba, Mamba2 and the SSD scan, with no behavior change. No CUDA host was available: the CUDA-gated tests return early on Metal and still need a CUDA run. Closes #3125
The fixture's feed-forward is MoE, and MLX's CPU GatherMM only accepts float32, so the all-f16 model aborted the whole lib test process on a build without a GPU backend. Return early when no GPU backend is present, matching the neighbouring CUDA-only test.
CUDA verification (GB10, sm_121)Ran the unchecked items from the test plan on CUDA-only tests: all pass, and all of them actually executed (no early return).
Fix pushed ( Prefill throughput:
Prefill is about 2.0x faster on the kernel, and decode is about 6% faster. The greedy output (32 tokens, reasoning channel) is byte-identical across all six runs and has no NaN or inf. The baseline is the graph scan on this branch, which is the path f16 Jamba took on |
CUDA verification procedure (GB10)This PR changes a CUDA kernel that is JIT-compiled at runtime, so nothing about it is verified until it runs on a CUDA device. Please run the steps below on the GB10 host and post the results as a comment on this PR. Do not merge, and do not change kernel code; if a step fails, report the output. Every command that touches the GPU must run under the host lock: 0. Checkout and buildgit fetch origin
git checkout fix/issue-3125-cuda-mamba1-f32-a
git pull --ff-only # head must be 5b88fac8 or later
cargo build --release --features cuda --bin mlxcel --bin mlxcel-bench-decode
1. Correctness testsgpu-lock run --tag pr3128 -- env RUST_MIN_STACK=16777216 \
cargo test -p mlxcel-core --features cuda --lib mamba1_scan -- --test-threads=1 --nocapture
gpu-lock run --tag pr3128 -- env RUST_MIN_STACK=16777216 \
cargo test -p mlxcel --features cuda --lib -- models::jamba models::mamba models::ssd_scan --test-threads=1Expected: every test passes, and the first command takes clearly longer than 0.00s (NVRTC compiles the kernel). The tests that matter most:
A bit-identity test fails rather than skips if the kernel rounds differently from the graph. If NVRTC rejects the source, the failure shows up here as a compile error in the test output; copy it in full. 2. Prefill throughput A/BModel: M=models/ai21-jamba-reasoning-3b-4bit
for len in 2048 8192; do
for round in 1 2; do
for arm in kernel graph; do
K=1; [ "$arm" = graph ] && K=0
gpu-lock run --tag pr3128 -- env MLXCEL_CUDA_F16_NORMALIZE=1 MLXCEL_MAMBA1_SCAN_KERNEL=$K \
./scripts/bench_decode.sh "$M" --prompt-tokens $len --max-tokens 32 \
--output benchmarks/pr3128-jamba-f16-$arm-$len-r$round.csv
done
done
doneReport a table of 3. Output equalityfor K in 1 0; do
gpu-lock run --tag pr3128 -- env MLXCEL_CUDA_F16_NORMALIZE=1 MLXCEL_MAMBA1_SCAN_KERNEL=$K \
./target/release/mlxcel generate -m "$M" \
-p "Explain the Mamba selective scan in three sentences." -n 64 --temp 0 > /tmp/pr3128-out-$K.txt 2>&1
done
diff <(grep -v '^\[Generated' /tmp/pr3128-out-1.txt) <(grep -v '^\[Generated' /tmp/pr3128-out-0.txt) && echo IDENTICALExpected: 4. New MLX pin compile check (separate from this PR)
git checkout origin/main
cargo build --release --features cuda --bin mlxcel
gpu-lock run --tag pr3128 -- ./target/release/mlxcel generate -m "$M" -p "Hello" -n 16 --temp 0Report whether the build succeeds and the binary generates. Then check this PR's branch out again if anything needs re-running. What to post backOne comment with: host, driver and CUDA version, the head commit tested, step 1 test results (pass/fail lines and timing), the step 2 table, the step 3 verdict and output, and the step 4 result. |
Summary
xwith the float32Athat f16 Mamba and Jamba keep (f16 cannot hold-exp(A_log)), so f16 Jamba on CUDA takes the kernel instead of the per-timestep graph scan.mamba1_scan_kernel_acceptsadmits exactly that one extra mix (every other input in f16). Metal and ROCm (float-state port) are untouched: the newTAtemplate arg is added only for the CUDA port.mamba1_decaymirrors what the graph scan does for that pair (decay_in_activation_dtypein jamba.rs,MambaBlock::ssm_stepin mamba.rs): MLXmultiplypromotes delta to float32 and rounds once (__fmul_rn),expruns on float32 (Exp{}), andastyperounds the decay to f16 (static_cast, as MLX's CastOp does). Uniform-dtype inputs run the same ops as before.ssm_a_f32andssm_a_from_logmove from mamba.rs into a newmodels::ssm_amodule; Mamba, Jamba, Mamba2, the SSD scan and their tests import from there. No behavior change.jamba_f16_model_with_an_overflowing_a_log_keeps_finite_logits(from fix(models): compute the SSM A from A_log in float32 in six more models #3122) now returns early without a GPU backend: its MoE fixture hits MLX's CPUGatherMM, which only takes float32, and aborted the whole CPU-only lib test process.Mamba / Falcon-Mamba still take the per-step graph scan on CUDA by design (their gate reads the float-state port; switching would change CUDA output, see
MambaBlock::forward). Their f16 scan inputs are now accepted by the predicate, which a test pins, but the Mamba forward path itself is unchanged.Test plan
mamba1_scan(mlxcel-core) andmodels::mamba,models::jamba,models::ssd_scanpass on Metal, also after merging current main; clippy--lib --tests -D warnings(both crates), fmt, and the env-var, native-env, kernel-port-dispatch and kernel-dtype-key checks pass.graph_exact_dtype_rule_admits_only_uniform_inputs_and_f16_with_float32_afails under both mutations tried (rule widened to any float32A; rule reverted to uniform only).--features cuda:mamba1_scan7 passed, includingcuda_kernel_is_bit_identical_to_the_graph_scan_for_f16_x_with_float32_aand the extendedcuda_kernel_declines_mixed_dtype_inputs;models::jamba models::mamba models::ssd_scan56 passed, 3 ignored, includingjamba_f16_cuda_kernel_matches_the_graph_scan_bit_for_bitandjamba_mamba_prefill_takes_the_fused_scan_where_a_port_exists. The JIT source compiles under NVRTC.ai21-jamba-reasoning-3b-4bitwithMLXCEL_CUDA_F16_NORMALIZE=1, 2151-token prompt, 3 runs each (details in the PR comment).MLXCEL_MAMBA1_SCAN_KERNEL=0)Prefill is about 2.0x faster and decode about 6% faster. Greedy output (32 tokens) is byte-identical across all six runs and finite. Not measured: an 8192-token prompt.
Closes #3125